#Lib
from synapse.platforms import *
from synapse.nvparams import *
#from string import *

serverAddr = '\x00\x00\x01'         # hard-coded address for Portal (PC/MAC)

#Global variables

ms = 30
sec_counter=0
knock2=0
coil=0
searching_state=False 

#status
led1 = 28                     #


"""
if platform != "SM700":
    compileError                    #script only valid on SM700
"""

#fuct to broadcast received data to portal or master
def sendData(mdata,id):
    #portAddr = '\x00\x00\x01'     # if no Master is found send Msg to portal 
    rpc(serverAddr,"logEvent",mdata,id)
      
def makeInput(pin):
    setPinDir(pin, False)          # set direction of the pin as input
    setPinPullup(pin, True)
    monitorPin(pin, True)          # Monitor for button press
  
    
def makeOutput(pin):
    setPinDir(pin, True)          # set direction of the pin as output
    writePin(pin, False)


    
def readPad(NUM):
   sendData(readAdc(10),"sp0")
   sendData(readAdc(11),"sp3")
   """ 
   string = str(readAdc(10))+','+str(readAdc(11))
   sendData(string,"sp9")"""
   
# Device address bits, 3 bits in total from DIP switch

# Things to do at startup
@setHook(HOOK_STARTUP)
def startupEvent():
    #LEDs
    global cnt
    cnt=0
    makeOutput(led1)
    
   
    
# Do every 100 MS  
@setHook(HOOK_100MS)
def timer100MSEvent(currentMs):
    global cnt
    
    if cnt%2==0:
      sendData(readAdc(10),'sp1')
    else:
      sendData(readAdc(11),'sp4 ')
    #readPad(cnt)
    cnt=cnt+1
    if (cnt==10):
      cnt =0    
